﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Configuration;
using System.Text.RegularExpressions;

namespace AMing.Mvc4.Core.Extension
{
    public static class HtmlHelperExtension
    {
        static string jscssVer = WebConfigurationManager.AppSettings["JsCssVer"];
        /// <summary>
        /// 生成引用Css的Html
        /// </summary>
        /// <param name="hepler"></param>
        /// <param name="cssurl"></param>
        /// <returns></returns>
        public static MvcHtmlString Css(this HtmlHelper hepler, string cssurl)
        {
            //cssurl = cssurl.UrlAddHostName();
            return MvcHtmlString.Create(string.Format("<link href=\"{0}?v={1}\" rel=\"stylesheet\" type=\"text/css\" />", cssurl, jscssVer));
        }
        /// <summary>
        /// 生成引用Css的Html(引用其他网站的)
        /// </summary>
        /// <param name="hepler"></param>
        /// <param name="cssurl"></param>
        /// <returns></returns>
        public static MvcHtmlString Css_Web(this HtmlHelper hepler, string cssurl)
        {
            return MvcHtmlString.Create(string.Format("<link href=\"{0}\" rel=\"stylesheet\" type=\"text/css\" />", cssurl));
        }
        /// <summary>
        /// 生成引用js的Html
        /// </summary>
        /// <param name="hepler"></param>
        /// <param name="jsurl"></param>
        /// <returns></returns>
        public static MvcHtmlString JavaScript(this HtmlHelper hepler, string jsurl)
        {
            //jsurl = jsurl.UrlAddHostName();
            return MvcHtmlString.Create(string.Format("<script src=\"{0}?v={1}\" type=\"text/javascript\"></script>", jsurl, jscssVer));
        }
        /// <summary>
        /// 生成引用js的Html(引用其他网站的)
        /// </summary>
        /// <param name="hepler"></param>
        /// <param name="jsurl"></param>
        /// <returns></returns>
        public static MvcHtmlString JavaScript_Web(this HtmlHelper hepler, string jsurl)
        {
            return MvcHtmlString.Create(string.Format("<script src=\"{0}\" type=\"text/javascript\"></script>", jsurl));
        }

        /// <summary>
        /// 生成引用js的Html
        /// </summary>
        /// <param name="hepler"></param>
        /// <param name="jsurl"></param>
        /// <param name="deubgjsurl"></param>
        /// <returns></returns>
        public static MvcHtmlString JavaScript(this HtmlHelper hepler, string jsurl, string deubgjsurl)
        {
            //jsurl = jsurl.UrlAddHostName();
#if DEBUG
            jsurl = deubgjsurl;
#endif
            return JavaScript(hepler, jsurl);
        }
        /// <summary>
        /// 生成引用Css的Html
        /// </summary>
        /// <param name="hepler"></param>
        /// <param name="cssurl"></param>
        /// <returns></returns>
        public static MvcHtmlString Css(this HtmlHelper hepler, string cssurl, string deubgcssurl)
        {
            //cssurl = cssurl.UrlAddHostName();
#if DEBUG
            cssurl = deubgcssurl;
#endif
            return Css(hepler, cssurl);
        }


        /// <summary>
        /// 将c#的特殊字符转成Html
        /// </summary>
        /// <param name="hepler"></param>
        /// <param name="jsurl"></param>
        /// <returns></returns>
        public static MvcHtmlString CSharpToHtml(this HtmlHelper hepler, string txt)
        {
            txt = txt.Replace("\n", "<br />");
            return MvcHtmlString.Create(txt);
        }

        /// <summary>
        /// 标签 转成超连接
        /// </summary>
        /// <param name="hepler"></param>
        /// <param name="tagString"></param>
        /// <returns></returns>
        public static MvcHtmlString TagToLink(this HtmlHelper hepler, string tagString, string strFromat, string aplit)
        {
            string[] tags = tagString.Split(',');
            StringBuilder writeHtml = new StringBuilder();
            bool isFirst = true;
            foreach (string item in tags)
            {
                if (!isFirst)
                {
                    writeHtml.AppendFormat(aplit);
                }
                isFirst = false;
                if (!string.IsNullOrWhiteSpace(item))
                {
                    writeHtml.AppendFormat(strFromat, item);
                }
            }
            return MvcHtmlString.Create(writeHtml.ToString());
        }

        /// <summary>
        /// 搜索内容 高亮 关键字
        /// </summary>
        /// <param name="hepler"></param>
        /// <param name="tagString"></param>
        /// <returns></returns>
        public static MvcHtmlString SearchKeyword(this HtmlHelper hepler, string content, string keyword, string fromat)
        {
            List<string> keyitem = new List<string>();
            System.Text.RegularExpressions.MatchCollection matchCollection = Regex.Matches(content, keyword, RegexOptions.IgnoreCase);//忽略大小写搜索字符串中的关键字
            for (int i = 0; i < matchCollection.Count; i++)
            {
                if (!keyitem.Contains(matchCollection[i].Value))
                {
                    keyitem.Add(matchCollection[i].Value);
                }
            }

            foreach (string item in keyitem)
            {
                string html = fromat.ToFromat(item);
                content = content.Replace(item, html);
            }

            return MvcHtmlString.Create(content);
        }

    }
}
